//
//  LPShowImagesView.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/9.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPShowImagesView.h"

@implementation LPShowImagesView

- (instancetype)initWithFrame:(CGRect)frame images:(NSArray *)images
{
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat width = ([UIScreen zj_screenWith] - 60 * [UIScreen zj_screenWScale]) / 4;
        NSInteger index;
        if (images.count > 8) {
            index = 9;
        }else{
            index = images.count + 1;
        }
        for (int i = 0; i < index; i ++) {
            NSInteger yu = i % 4;
            NSInteger zheng = i / 4;
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((width + [UIScreen zj_screenWScale] * 10) * yu, (width + 10 * [UIScreen zj_screenHScale]) * zheng, width, width)];
            imageView.userInteractionEnabled = YES;
            imageView.tag = i;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
            [imageView addGestureRecognizer:tap];
            
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(imageView.width - 20 * [UIScreen zj_screenWScale], 0, 20 * [UIScreen zj_screenWScale], 20 * [UIScreen zj_screenWScale])];
            [button setBackgroundImage:[UIImage imageNamed:@"voice_close"] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = i;
            
            if (i == images.count) {
                imageView.image = [UIImage imageNamed:@"btn_addimg2"];
            }else{
                imageView.image = images[i][@"image"];
                [imageView addSubview:button];
            }
            [self addSubview:imageView];

        }
    }
    return self;
}


- (void)tapClick:(UITapGestureRecognizer *)tap{
    if (self.delegate) {
        [self.delegate lpShowImageWithView:self clickImageVIewindex:tap.view.tag];
    }
}

- (void)buttonClick:(UIButton *)sender{
    if (self.delegate) {
        [self.delegate lpShowImageWithView:self clickButtonindex:sender.tag];
    }

}
@end
